﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;

namespace WebStudio.Mvc.Core.Extensions
{
    /// <summary>
    /// 多选框列表扩展
    /// </summary>
    public static class CheckBoxListExtension
    {
        #region  多选框列表扩展重载
        /// <summary>
        /// 多选框列表扩展
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="name"></param>
        /// <param name="items"></param>
        /// <returns></returns>
        public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IDictionary<string, string> items)
        {
            return CheckBoxList(helper, name, items, null, null);
        }

        /// <summary>
        /// 多选框列表扩展
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="name"></param>
        /// <param name="items"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IDictionary<string, string> items, object htmlAttributes)
        {
            return CheckBoxList(helper, name, items, null, htmlAttributes);
        }

        /// <summary>
        /// 多选框列表扩展
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="name"></param>
        /// <param name="items"></param>
        /// <param name="selectedValues"></param>
        /// <returns></returns>
        public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IDictionary<string, string> items, IEnumerable<string> selectedValues)
        {
            return CheckBoxList(helper, name, items, selectedValues, null);
        }

        /// <summary>
        /// 多选框列表扩展
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="name"></param>
        /// <param name="items"></param>
        /// <param name="selectedValues"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IDictionary<string, string> items, IEnumerable<string> selectedValues, object htmlAttributes)
        {
            var selectListItems = from i in items
                                  select new SelectListItem
                                             {
                                                 Text = i.Key,
                                                 Value = i.Value,
                                                 Selected = (selectedValues != null && selectedValues.Contains(i.Value))
                                             };
            return CheckBoxList(helper, name, selectListItems, htmlAttributes);
        }

        /// <summary>
        /// 多选框列表扩展
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="name"></param>
        /// <param name="items"></param>
        /// <returns></returns>
        public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IEnumerable<SelectListItem> items)
        {
            return CheckBoxList(helper, name, items, null);
        }
        #endregion

        #region 多选框列表扩展
        /// <summary>
        /// 多选框列表扩展
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="name"></param>
        /// <param name="items"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IEnumerable<SelectListItem> items, object htmlAttributes)
        {
            var output = new StringBuilder();
            output.Append("<ul class='checkbox'>");
            foreach (var item in items)
            {
                var id = string.Format("{0}_{1}", name, item.Value);
                output.Append("<li>");
                var checkboxList = new TagBuilder("input");
                checkboxList.MergeAttribute("id", id);
                checkboxList.MergeAttribute("type", "checkbox");
                checkboxList.MergeAttribute("name", name);
                checkboxList.MergeAttribute("value", item.Value);
                // Check to see if it's checked
                if (item.Selected)
                    checkboxList.MergeAttribute("checked", "checked");
                // Add any attributes
                if (htmlAttributes != null)
                {
                    //TODO:多选列表htmlAttributes应用
                    //checkboxList.MergeAttributes(htmlAttributes);
                }
                checkboxList.SetInnerText(item.Text);
                output.Append(checkboxList.ToString(TagRenderMode.SelfClosing));
                output.AppendFormat("<label for=\"{1}\">{0}</label></li>", item.Text, id);
            }
            output.Append("</ul>");
            return MvcHtmlString.Create(output.ToString());
        }
        #endregion
    }
}
